package com.cartoonrabbit.controller;


import com.cartoonrabbit.service.IRechargeOrderService;
import com.cartoonrabbit.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiuTeng
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/recharge/order")
@Api(tags = "充值相关接口")
public class RechargeOrderController {

    @Autowired
    private IRechargeOrderService rechargeOrderService;

    // 预支付
    @ApiOperation("制作次数充值")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/prePay")
    public RespBean prePay(Long money, Principal principal) throws Exception {

        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }

        if (ObjectUtils.isEmpty(money)||money<=0){
            return RespBean.fail("金额不能为空");
        }

        return rechargeOrderService.doPrePay(money,principal.getName());
    }

    @ApiOperation("支付通知接口")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/notify/{orderId}/{status}")
    public RespBean notify(@PathVariable String orderId,@PathVariable Integer status, Principal principal) throws Exception {

        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }

        if (ObjectUtils.isEmpty(orderId)||ObjectUtils.isEmpty(status)){
            return RespBean.fail(500,"参数异常");
        }

        return rechargeOrderService.doNotify(orderId,status,principal.getName());
    }

    @ApiOperation("获取充值明细")
    @GetMapping("/list")
    @PreAuthorize("@ss.permitAll()")
    public RespBean getRechargeOrderList(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size,
                                         Principal principal){
        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }
        return rechargeOrderService.getRechargeOrderList(currentPage,size,principal.getName());
    }
}
